<?php
namespace Home\Controller;
use Think\Controller;
use Common\Controller\BaseController;


class LoginController extends BaseController {
    public function index(){
        if(session("username")){
            $this->redirect("/admin/panel");
        }
        $this->display();
    }

    public function verify(){
        $config =    array(
            'codeSet' => "1234567890",
            'fontSize'    =>    16,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    private function isPasswdMatched($userName, $passwd){
        $user = M("user")->where(array('username'=>$userName))->find();
        $passwd = md5($passwd);
        if($user && $user['passwd']==$passwd){
            return true;
        }
        return false;
    }
    //
    public function loginCheck(){
        $userName = I("post.username");
        $passwd = I("post.passwd");
        $verifyCode = I("post.passwd2");
        $verify = new \Think\Verify();
        if(!$verify->check($verifyCode)){
            $this->error("验证码输入错误");
        }

        if($this->isPasswdMatched($userName, $passwd)){
            session("username", $userName);
            $this->success("登陆成功");
        }
        $this->error('用户名与密码不匹配');
    }
}